package com.jams.goodox.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hangzhou.jin.customview.TailsTextView;
import com.jams.goodox.R;
import com.jams.goodox.activity.mine.bill.pinnedheaderlistview.SectionedBaseAdapter;
import com.jams.goodox.entity.Bill;
import com.jams.goodox.entity.BillEntity;
import com.jams.library.fresco.FrescoUtil;

import java.util.List;

/**
 * Created by Jin on 2016/7/15.
 * Description
 */
public class MineBillAdapter   extends SectionedBaseAdapter {

    private Context context;

    private List<BillEntity.Parent> list;

    public MineBillAdapter(Context context, List<BillEntity.Parent> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Object getItem(int section, int position) {
        return null;
    }

    @Override
    public long getItemId(int section, int position) {
        return 0;
    }

    @Override
    public int getSectionCount() {
        return list.size();
    }

    @Override
    public int getCountForSection(int section) {
        return list.get(section).getData().size();
    }

    class ChildView {
        TextView date1, date2, content2;
        TailsTextView content1;
        SimpleDraweeView img;
    }

    @Override
    public View getItemView(int section, int position, View convertView, ViewGroup parent) {
        ChildView childView;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.mine_bill_child_content, null);
            childView = new ChildView();
            childView.date1 = (TextView) convertView.findViewById(R.id.date1);
            childView.date2 = (TextView) convertView.findViewById(R.id.date2);
            childView.content1 = (TailsTextView) convertView.findViewById(R.id.content1);
            childView.content2 = (TextView) convertView.findViewById(R.id.content2);
            childView.img = (SimpleDraweeView) convertView.findViewById(R.id.img);
            convertView.setTag(childView);
        } else {
            childView = (ChildView) convertView.getTag();
        }

        if (list.size() > section) {
            Bill.DataBean.ResultListBean rlb = list.get(section).getData().get(position);

            childView.date1.setText(rlb.getPayType(context));
            childView.date2.setText(rlb.getDate1());

            if (rlb.getType() == 2) {
                childView.content2.setText(getTypeStr(rlb));
            } else if (rlb.getType() == 1) {
                childView.content2.setText("充值");
            } else {
                childView.content2.setText("退款-" + rlb.getSubName());
            }
            childView.content1.setText(rlb.getMoneyChangeStr());
            if (rlb.getSubPic() != null)
                FrescoUtil.loadNetUrl(childView.img, rlb.getSubPic());
            else
                FrescoUtil.loadNetUrl(childView.img, "w");
        }

        return convertView;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        TextView title;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.mine_bill_child_title, null);
            title = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(title);
        } else {
            title = (TextView) convertView.getTag();
        }

        if (list.size() > section) {
            String date = list.get(section).getDate();
            title.setText(date.substring(0, 4) + "-" + date.substring(4, 6));
        }
        return convertView;
    }

    private String getTypeStr(Bill.DataBean.ResultListBean rlb) {
        switch (rlb.getSpecialType()) {
            case 43:
                return "消费-物业缴费";
            case 23:
                return "消费-猎头佣金";
            case 45:
                return "消费-停车申请";
            case 54:
                return "消费-场地预订";
            case 24:
                return "消费-扫码付款";
            default:
                return "消费-" + rlb.getSubName();
        }
    }
}
